package com.ziroom.crm.mq.core.support;

import com.ziroom.crm.mq.core.context.InvokeContext;
import com.ziroom.crm.mq.core.data.Message;
import com.ziroom.crm.mq.core.node.NodeProvider;
import com.ziroom.crm.mq.core.processor.*;

import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author WeiShaoying
 * @version 1.0
 * @date Created in 2020年09月01日 17:30
 * @since 1.0
 */
@SuppressWarnings("ALL")
class ProcessorInvoker {

    private ProcessorInvoker() {
    }

    /**
     * 执行前置触发
     *
     * @param queueWrapper
     * @param message
     */
    protected static void preSendInvoke(QueueWrapper queueWrapper, Message message) {
        List<SendProcessor> sendProcessorList = ProcessorFactory.getSendProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (SendProcessor sendProcessor : sendProcessorList) {
            sendProcessor.preSend(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行后置处理
     *
     * @param queueWrapper
     * @param message
     */
    protected static void postSendInvoke(QueueWrapper queueWrapper, Message message) {
        List<SendProcessor> sendProcessorList = ProcessorFactory.getSendProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (SendProcessor sendProcessor : sendProcessorList) {
            sendProcessor.postSend(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行重发前置处理
     *
     * @param queueWrapper
     * @param message
     */
    protected static void preReSendInvoke(QueueWrapper queueWrapper, Message message) {
        List<ReSendProcessor> reSendProcessorList = ProcessorFactory.getReSendProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (ReSendProcessor reSendProcessor : reSendProcessorList) {
            reSendProcessor.preReSend(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行重发后置处理
     *
     * @param queueWrapper
     * @param message
     */
    protected static void postReSendInvoke(QueueWrapper queueWrapper, Message message) {
        List<ReSendProcessor> reSendProcessorList = ProcessorFactory.getReSendProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (ReSendProcessor reSendProcessor : reSendProcessorList) {
            reSendProcessor.postReSend(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行前置触发
     *
     * @param queueWrapper
     * @param message
     */
    protected static void preConsumeInvoke(QueueWrapper queueWrapper, Message message) {
        List<ConsumeProcessor> reSendProcessorList = ProcessorFactory.getConsumeProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (ConsumeProcessor consumeProcessor : reSendProcessorList) {
            consumeProcessor.preConsume(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行后置处理
     *
     * @param queueWrapper
     * @param message
     */
    protected static void postConsumeInvoke(QueueWrapper queueWrapper, Message message) {
        List<ConsumeProcessor> reConsumeProcessorList = ProcessorFactory.getConsumeProcessorList();

        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (ConsumeProcessor consumeProcessor : reConsumeProcessorList) {
            consumeProcessor.postConsume(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行重发前置处理
     *
     * @param queueWrapper
     * @param message
     */
    protected static void preReConsumeInvoke(QueueWrapper queueWrapper, Message message) {
        List<ReConsumeProcessor> reSendProcessorList = ProcessorFactory.getReConsumeProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (ReConsumeProcessor reConsumeProcessor : reSendProcessorList) {
            reConsumeProcessor.preReConsume(invokeContext, queueWrapper);
        }
    }

    /**
     * 执行重发后置处理
     *
     * @param queueWrapper
     * @param message
     */
    protected static void postReConsumeInvoke(QueueWrapper queueWrapper, Message message) {
        List<ReConsumeProcessor> processorList = ProcessorFactory.getReConsumeProcessorList();
        InvokeContext invokeContext = InvokeContext.builder()
                .message(message)
                .node(NodeProvider.getNode(queueWrapper))
                .build();
        for (ReConsumeProcessor reConsumeProcessor : processorList) {
            reConsumeProcessor.postReConsume(invokeContext, queueWrapper);
        }
    }
}
